package com.gin.no_gay_alliance.utils;

import static com.gin.nga.enums.ReplyStatus.LOCKED;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.enums.ReplyStatus;
import com.gin.nga.enums.TitleColor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/**
 * 主题/回复的状态工具类
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/11 16:49
 **/
public class ReplyStatusUtils {

    /**
     * 配置主题的状态信息
     *
     * @param mirror     是否为镜像贴
     * @param replyOnce  是否为单帖回复
     * @param statusList 某主题或回复的状态列表
     * @param group      用于显示状态的组件
     * @param context    上下文
     */
    public static void configTopic(boolean mirror, boolean replyOnce, List<ReplyStatus> statusList, ViewGroup group, Context context) {
        config(mirror, replyOnce, statusList, group, context, ReplyStatus.LOCKED, ReplyStatus.HIDDEN, ReplyStatus.VERIFYING, ReplyStatus.NOT_VERIFIED, ReplyStatus.FREE_EDIT, ReplyStatus.SELF_REPLY, ReplyStatus.HIDE_TITLE, ReplyStatus.IS_SET);
    }

    /**
     * 配置回复的状态信息
     *
     * @param statusList 某主题或回复的状态列表
     * @param group      用于显示状态的组件
     * @param context    上下文
     */
    public static void configReply(List<ReplyStatus> statusList, ViewGroup group, Context context) {
        config(false, false, statusList, group, context, ReplyStatus.LOCKED, ReplyStatus.HIDE_TITLE, ReplyStatus.HIDDEN, ReplyStatus.VERIFYING, ReplyStatus.NOT_VERIFIED, ReplyStatus.NO_HINT, ReplyStatus.FREE_EDIT);
    }

    /**
     * 配置状态信息
     *
     * @param mirror     是否为镜像贴
     * @param replyOnce  是否为单帖
     * @param statusList 某主题或回复的状态列表
     * @param group      用于显示状态的组件
     * @param context    上下文
     * @param statuses   希望显示的状态
     */
    public static void config(boolean mirror, boolean replyOnce, List<ReplyStatus> statusList, ViewGroup group, Context context, ReplyStatus... statuses) {
        group.removeAllViews();
        if ((ObjectUtils.isEmpty(statusList) || ObjectUtils.isEmpty(statuses)) && !replyOnce) {
            // 如果列表为空 隐藏组件，清理子View
            group.setVisibility(View.GONE);
            return;
        }
        // 希望显示的状态
        List<ReplyStatus> replyStatuses = Arrays.asList(statuses);
        // 需要显示的状态
        statusList = statusList != null ? statusList : new ArrayList<>();
        List<ReplyStatus> list = statusList.stream().filter(replyStatuses::contains).collect(Collectors.toList());

        addTextView(list, group, context, LOCKED, TitleColor.RED);
        addTextView(list, group, context, ReplyStatus.HIDDEN, TitleColor.RED);
        addTextView(list, group, context, ReplyStatus.VERIFYING, TitleColor.RED);
        addTextView(list, group, context, ReplyStatus.NOT_VERIFIED, TitleColor.RED);
        addTextView(list, group, context, ReplyStatus.HIDE_TITLE, TitleColor.RED);

        addTextView(list, group, context, ReplyStatus.IS_COMMENT, TitleColor.BLUE);
        addTextView(list, group, context, ReplyStatus.NO_HINT, TitleColor.BLUE);
        addTextView(list, group, context, ReplyStatus.FREE_EDIT, TitleColor.BLUE);
        addTextView(list, group, context, ReplyStatus.SELF_REPLY, TitleColor.BLUE);
        addTextView(list, group, context, ReplyStatus.IS_SET, TitleColor.GREEN);

        if (replyOnce) {
            TextView textView = new TextView(context);
            textView.setText("[单帖]");
            textView.setTextColor(Color.parseColor("#" + TitleColor.BLUE.rgb));
            group.addView(textView);
        }
        if (mirror) {
            TextView textView = new TextView(context);
            textView.setText("[镜像]");
            textView.setTextColor(Color.parseColor("#" + TitleColor.BLUE.rgb));
            group.addView(textView);
        }

        // 控制容器的显示状态
        if (group.getChildCount() == 0) {
            group.setVisibility(View.GONE);
        } else {
            group.setVisibility(View.VISIBLE);
        }


    }


    /**
     * 判断列表中是否存在指定状态，若存在，创建TextView放入
     *
     * @param statusList 状态列表
     * @param group      用于显示状态的组件
     * @param context    上下文
     * @param status     指定状态
     * @param color      字体颜色
     */
    private static void addTextView(List<ReplyStatus> statusList, ViewGroup group, Context context, ReplyStatus status, TitleColor color) {
        if (statusList.contains(status)) {
            TextView textView = new TextView(context);
            textView.setText(String.format(Locale.CHINA, "[%s]", status.name));
            textView.setTextColor(Color.parseColor("#" + color.rgb));
            group.addView(textView);
        }
    }
}
